<?php

namespace app\system\controller;

use think\Request;
use app\system\model\SysAdminModel;
use app\system\model\SysConfigModel;
use think\Controller;

class LoginSystem extends Controller
{

    public function doLogin(Request $request, SysAdminModel $sysAdminModel, SysConfigModel $sysConfigModel)
    {
        if ($request->isAjax()) {
            $username = $request->param("username");
            $pwd = $request->param("pwd");

            if (!empty($username) && !empty($pwd)) {
                $user = $sysAdminModel->getUserByName($username);
                if ($user != null) {
                    if ($user["pwd"] == md5($pwd)) {
                        // 登陆成功
                        session("admin_user",$user);
                        return json(["code" => 0, "msg" => "登陆成功"]);
                    } else {
                        return json(["code" => 1, "msg" => "密码错误!err:1"]);
                    }
                } else {
                    return json(["code" => 2, "msg" => "用户不存在!err:2"]);
                }
            }
        } else {
            $systemName = $sysConfigModel->getConfigValue("systemName");    // 系统名称
            $this->assign("systemName", $systemName);
            return $this->fetch();
        }
    }
}
